package com.teamwork;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class jsxxsc {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的教师信息的ID：");
        int tId = scanner.nextInt();
        scanner.close();

        deleteTeacher(tId);
    }

    public static void deleteTeacher(int tId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentexam", "root", "123456");

            // 准备SQL语句
            String sql = "DELETE FROM teacher WHERE TId = ?";

            // 创建PreparedStatement对象
            preparedStatement = connection.prepareStatement(sql);

            // 设置参数
            preparedStatement.setInt(1, tId);

            // 执行SQL语句
            int rowsAffected = preparedStatement.executeUpdate();

            // 输出结果
            if (rowsAffected > 0) {
                System.out.println("教师信息删除成功！");
            } else {
                System.out.println("教师信息删除失败，未找到对应ID的教师！");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
